package com.woniuxy.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.PageDto;
import com.woniuxy.entity.Address;
import com.woniuxy.entity.Orders;
import com.woniuxy.entity.Product;
import com.woniuxy.mapper.AddressMapper;
import com.woniuxy.mapper.OrderMapper;
import com.woniuxy.mapper.ProductMapper;
import com.woniuxy.service.LogService;
import com.woniuxy.service.OrderService;


@Service
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderMapper orderMapper; 
	@Autowired
	private AddressMapper addressMapper;
	@Autowired
	private ProductMapper productMapper;
	@Autowired 
	LogService logservice;
	
	
	@Override
	public PageDto<Orders> find(int uid,int page) {
		PageHelper.startPage(page,7);
		List<Orders> orders = orderMapper.find(uid);
		PageInfo<Orders> pageInfo = new PageInfo<>(orders);
		PageDto<Orders> pageDto = new PageDto<Orders>();
		pageDto.setCurrentPage(page);//当前页
		pageDto.setPageSize(7);//每一页大小
		pageDto.setTotal(pageInfo.getTotal());//总条数
		pageDto.setTotalPages(pageInfo.getPages()); // 总页数
		pageDto.setData(orders); // 商品数据
		return pageDto;
		
	}
	@Override
	public int del(int o_id) {
	
		return orderMapper.del(o_id)>0?1:-1;
	}
	//查找卖家订单(我的卖品)
	@Override
	public PageDto<Orders> findsellerorders(int u_id,int page) {
		
		logservice.addorderLog();
		PageHelper.startPage(page,7);
		List<Orders> orders = orderMapper.findsellerorders(u_id);
		PageInfo<Orders> pageInfo = new PageInfo<>(orders);
		PageDto<Orders> pageDto = new PageDto<Orders>();
		pageDto.setCurrentPage(page);//当前页
		pageDto.setPageSize(7);//每一页大小
		pageDto.setTotal(pageInfo.getTotal());//总条数
		pageDto.setTotalPages(pageInfo.getPages()); // 总页数
		pageDto.setData(orders); // 商品数据
		return pageDto;
	}
	
	
	//通过订单id查找订单详情
		public HashMap<String, Object> findorderdetail(int orderid,int u_id) {
			//通过订单id查找订单,通过商品id查找商品详情
			Orders orders = orderMapper.findorderdetail(orderid);
			Product product = productMapper.find(orders.getP_id());
			//通过用户id查找收货地址
			List<Address> addresses = addressMapper.findAddressByUid(u_id);
			HashMap<String, Object> map = new HashMap<>();
			map.put("product", product);
			map.put("addresses", addresses);
			map.put("orders", orders);
			return map;
			
		};
	
}
